package com.fingard.dsp.bank.directbank.sdccba01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 山东城商银行联盟联盟
 * Created by cludy on 2020.4.24.
 */
public class SDCCBA01Balance extends SDCCBA01Base implements IBalance {

    /**
     * 查询余额
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try {
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = new RetAtsBalDetail();
                //======拼接报文======
                //拼接请求报文体
                SDCCBA01Header header = new SDCCBA01Header();
                header.opname = "enterpriseQry";
                header.serialNo = getQryTransID();
                XmlTextWriter xmlReq = new XmlTextWriter();
                writeReqHeader(xmlReq, header);
                XElement body = new XElement("ReqParam", "");
                body.addChild(new XElement("accountNo", p_req7506.readedDetail.actNum));//账号
                body.addChild(new XElement("currencyType", convertCurToBank(p_req7506.readedDetail.actCur)));//币种
                body.addChild(new XElement("currIden", "0"));//钞汇表示
                xmlReq.writeXElement(body);
                xmlReq.writeEndDocument();
                //获取签名数据
                String sign = signnatrue(xmlReq.toXmlString());
                //String sign="1111";
                //拼接请求报文
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                header.signedData = sign;
                writeCommonHeader(xmlTextWriter, header);
                xmlTextWriter.directWrite(xmlReq.toXmlString());
                xmlTextWriter.writeEndElement();
                //======拼接报文======
                //发送请求报文
                String[] bankResult = sendToBank(requestBody(header, xmlTextWriter.toXmlString()), p_req7506.reqHeader.transType, p_req7506.reqHeader.transType);
                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSb = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);
                    if (tmpBkReader != null) {
                        String respCode = tmpBkReader.getFirstText("/BankData/opRep/hostReturnCode");
                        String respInfo = tmpBkReader.getFirstText("/BankData/opRep/hostErrorMessage");
                        if (success(respCode)) {//交易成功
                            if (tmpBkReader.read("/BankData/opRep/RepParam")) {
                                detail.respCode = "0";
                                detail.respInfo = respInfo;
                                detail.actCur = p_req7506.readedDetail.actCur;
                                detail.actNum = p_req7506.readedDetail.actNum;
                                detail.availBal = delZero(tmpBkReader.readedXElement.getSingleTextNotNull("balanceAvailable"));
                                detail.currBal = delZero(tmpBkReader.readedXElement.getSingleTextNotNull("balance"));
                                detail.frozenBal = delZero(tmpBkReader.readedXElement.getSingleTextNotNull("balanceFreeze"));
                            }
                        } else {
                            detail.respCode = "0";
                            detail.respInfo = respInfo;
                        }
                    } else {
                        detail.respCode = "0";
                        detail.respInfo = tmpSb.toString();
                    }
                } else {
                    detail.respCode = "0";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
